#!/usr/bin/env pytho
# -*- coding:utf-8 -*-
import pymysql
import wx
import sys
import re
import time
###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):
    
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 640,480 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        
        self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP, wx.ID_ANY )
        
        self.Centre( wx.BOTH )
    
    def __del__( self ):
        pass
    

###########################################################################
## Class MyPanel1
###########################################################################

class MyPanel1 ( wx.Panel ):
    
    def __init__( self, parent ):
        wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.TAB_TRAVERSAL )
        
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        
        self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE )
        bSizer1.Add( self.m_textCtrl1, 1, wx.ALL|wx.EXPAND, 5 )
        
        self.m_button1 = wx.Button( self, wx.ID_ANY, u"读取", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_button1, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )
        
        
        self.SetSizer( bSizer1 )
        self.Layout()

        self.m_button1.Bind( wx.EVT_BUTTON, self.onc )

        
    def __del__( self ):
        pass

    def onc( self, event ):
        self.scan()
        event.Skip()

    def scan(self):
        
        conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='demhghc', db='zthubo', charset='utf8')
        # 创建游标
        cur = conn.cursor()

        #cur.close()

        dict={}
        file=open('C:\\ZfNetwork\\YNCZWLServer\\techer.log',"r")
        lines=file.readlines()
        file.close()
        self.m_textCtrl1.SetValue("")
        for line in lines:
            m=line.split("，")
            d=m[2].split(",")
            if(len(d)==7):
                dict[d[1]]=d[6]
        for key,values in  dict.items():
            sql= "select * from class_seatnum where `ExamID`='{f}'" .format(f=key)
            cur.execute(sql)
            results = cur.fetchone()
            if(results!=None):
                v=int(re.search('\d+',values,re.I).group())
                if(results[6]==v):
                    s="OK"
                else:
                    s="error"
                print(s,"\t",results[2],results[0],"\t",results[6],"\t",values)
                self.m_textCtrl1.AppendText("%s\t%s\t%s\t%s\t%s\t%s"%(s,results[2],results[0],results[1],results[6],values))
            else:
                sql= "select * from examid where `ID`={f}" .format(f=key)

                cur.execute(sql)
                results1 = cur.fetchone()
                if(results1==None):
                    print("error")
                    self.m_textCtrl1.AppendText("%s\t%s\t error" %(key,values))
                else:
                    self.m_textCtrl1.AppendText("%s\t%s\t%s" %(key,results1[1],values))
                    print("%s\t%s\t%s " %(key,results1[1],values))  
                

            
        '''
        sql="select * from class_seatnum where `ExamID`='"+d[1]+"'"
        #print(sql)
        result = cursor.execute(sql)
        row_1 = cursor.fetchone()
        print (row_1[0],row_1[2],row_1[6])
        '''
        cur.close()
        conn.close()
app=wx.App()
w=MyFrame1(None)
p=MyPanel1(w)
w.Show()
app.MainLoop()
